//
//  WLSTabBar.swift
//  1-微博
//
//  Created by 王隆善 on 16/7/28.
//  Copyright © 2016年 王隆善. All rights reserved.
//

import UIKit

class WLSTabBar: UITabBar {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //添加子控件
        setupUI()
        
    }
    
    
    //使用storyBoard时会调用这个方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加子控件
    private func setupUI()
    {
        addSubview(composeButton)
    }
    
    
    //MARK:重写layoutSubViews
    override func layoutSubviews() {
        //调用父类
        super.layoutSubviews()
        
        //修改子控件的宽和起始位置
        let width = bounds.width / 5
        let height = bounds.height
        
        //定义一个索引,用于标记对哪一个控件进行设置frame
        var index = 0
        
        for subview in subviews
        {
            //判断类型
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                subview.frame = CGRectMake(CGFloat(index)*width, bounds.origin.y, width, height)
                
                //修改索引值
                index++
                
                //判断索引
                if index == 2
                {
                    index++
                }
            }
        }
        
        //设置中间的点击button的frame
        composeButton.frame = CGRectMake(2*width, bounds.origin.y, width, height)
    }
    
    
    //MARK:--懒加载控件
    lazy var composeButton: UIButton = {
    
        let button = UIButton()
        
        //设置图片
    button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
    button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        
        //设置背景图片
    button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        
    button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    func click()
    {
        print("点击了")
    }

}
